<?php

class Jd_task_jd_list_pool_model extends MY_Model {





    public function __construct()
    {
        $this->modelename='Jd_task_jd_list_model';
        $this->tablename='task_jd_list_pool';
        parent::__construct();
    }


    /**
     *
     * 将任务子单加入任务池
     * @param array $array
     * @return INT
     */
    public function  addtasklisttopoll($array=array()){
        try
        {
           return  @$r= $this->add($this->tablename,$array);
        }catch(Exception $e)
        {
            return false;
        }


    }


    /**
     * 刷手从任务池获取任务
     */
    public function  HunterGetTask($epage,$Exclude="",$type=1){
        $sql="select * from jd_task_jd_list_pool  where  `type`=$type  AND  `status` =0  AND `hunter_id`=0  AND `shop_name` <>'' group by `shop_name` limit 0,".$epage;
        if(!empty($Exclude)){//重新获取时排除之前的商铺
            foreach ($Exclude as $id=>$v){
                $str[]=" AND  `shop_name`<>  '$v'";
            }
            $str=implode($str," ");
            $sql="select * from jd_task_jd_list_pool  where `type`=$type  AND   `status` =0  AND `hunter_id`=0  AND `shop_name`<>''  $str    group by `shop_name` limit 0,".$epage;
        }
        $result=  $this->GetBySql($sql);
        $row['list']=$result;
        $row['count']=count($result);
         return $row;
    }


    /**
     *
     * 刷手从任务池中更新标记该任务为自己的任务
     * @param $user_id
     * @param array $array
     */
    public  function HunterAccept($user_id,$array=array(),$awz){
         if(empty($array)){
             return false;
         }
        $sql="update  jd_"."$this->tablename set `status`=1,hunter_id=$user_id ,awz='$awz' where  `status`=0 AND  id in  (".implode($array,",").")";
        $r= $this->db->query($sql);
        return$r;
    }


    /**
     * 刷手完成任务
     */
    public function  finishtask($user_id,$array=array()){
          $real_price = $array['real_price'];
          $id =$array['id'];
          $orderid = $array['orderid'];
          $awz=isset($array['awz'])?$array['awz']:"";

          $result = $this->db->select('listid,status,hunter_id,awz')->from('jd_task_jd_list_pool')->where("id=".$id)->get()->result_array();
          if($result[0]['status']==2 || $result[0]['hunter_id']!=$this->userId){
              return false;
          }
          $sql="update  jd_"."$this->tablename set `status`=2,hunter_id=$user_id, `real_price`=$real_price , `orderid`='$orderid' where id =$id";
          $r= $this->db->query($sql);
          if(!$r){
              return false;
          }
          $id=$result[0]['listid'];
          $finishtime=time();
          $team=$array['team'];
          $huntername=$array['username'];
          if(empty($awz)){
              $awz=$result[0]['awz'];
          }
          $sql="update  jd_task_jd_list set `status`=2,hunter_id=$user_id, `real_price`=$real_price ,`awz`='$awz',  `orderid`='$orderid' ,`finishtime`=$finishtime,`team`='$team',hunter_name='$huntername'  where id =$id";
          $r= $this->db->query($sql);
          return$r;
    }


    /**
     * 刷手放弃任务 任务重新可以被其他人获取
     */
    public function  replacetask($idstr){
        $idstr=implode($idstr,",");
        $user_id=$this->userId;
        $sql="update  jd_task_jd_list_pool set `status`=0 , hunter_id=0, awz='' where hunter_id=$user_id  AND id in (".$idstr.") AND status<2";
        $r= $this->db->query($sql);
        return$r;
    }


    public function truncatelog(){
        $sql="truncate  jd_task_jd_list_pool ";
        $r= $this->db->query($sql);
    }

    public function taskupdate($row)
    {
        $table=$this->tablename;
        $primary="taskid";
        $id=$row['id'];
        unset($row['id']);
        return $this->update($table, $primary, $id, $row,10000); // TODO: Change the autogenerated stub
    }

    /**
     * 重置任务状态为初始
     * @param $id
     * @return mixed
     */
    public function ResetStatus($id){
        $sql="update  jd_task_jd_list_pool  set `status`= 0  ,hunter_id=0,awz='' where taskid =$id  AND hunter_id>0 AND  `status`=1 ";
        return $r= $this->db->query($sql);
    }

    /**
     * 废弃未被接取任务
     * @param $id
     * @return mixed
     */
    public function CancelStatus($id){
        $sql="update jd_task_jd_list_pool  set `status`= 3  ,hunter_id=0 where taskid =$id  AND hunter_id=0 AND  `status`=0 ";
        return $r= $this->db->query($sql);
    }


    public function getpoolsnumber(){
        $sql="select  `type`, count(1) as 'num' from jd_task_jd_list_pool  where   `status` <2 AND    `type`>0  group by `type` ";
        return $result=  $this->GetBySql($sql);
    }





}
